﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace GuiaAeropuerto.ViewModel
{
   public class GuiaAeropuertoViewModel:NotificationObject
    {

       public event EventHandler<AirportResponseReadyEventArg> responseReady;

        private int zoom;

        public int Zoom
        {
            get { return zoom; }
            set { zoom = value;
            OnPropertyChanged(this);
            }
        }

        private List<Airport> aeropuertos;

        public List<Airport> Aeropuertos
        {
            get { return aeropuertos; }
            set { aeropuertos = value;
            OnPropertyChanged(this);
            }
        }

        private string pais;

        public string Pais 
        {
            get { return pais; }
            set { pais = value;
            OnPropertyChanged(this);
            }
        }

        private string color;

        public string Color
        {
            get { return color; }
            set { color = value;
            OnPropertyChanged(this);
            }
        }

        private string cartografia;

        public string Cartografia
        {
            get { return cartografia; }
            set { cartografia = value;
            OnPropertyChanged(this);
            }
        }
        

        
        
        

        public GuiaAeropuertoViewModel() {
            Zoom = 15;
            AirportService service = new AirportService();
            service.responseReady += service_responseReady;
            LoadSettings();
            service.getAirportsByCountry(Pais);
        }

        void service_responseReady(object sender, AirportResponseReadyEventArg e)
        {
            if (responseReady != null)
            {
                responseReady(this, e);
            }
        }

        private void LoadSettings()
        {
            System.IO.IsolatedStorage.IsolatedStorageSettings settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
            if (settings.Contains("Pais"))
            {
                Pais = settings["Pais"].ToString();
            }
            else {
                Pais = "Venezuela";
            }

            if (settings.Contains("Color"))
            {
                Color = settings["Color"].ToString();
            }
            else
            {
                Color = "Claro";
            }

            if (settings.Contains("Cartografia"))
            {
                Cartografia = settings["Cartografia"].ToString();
            }
            else
            {
                Cartografia = "Hibrido";
            }


           
        }

        public void UpdateSettings()
        {
            System.IO.IsolatedStorage.IsolatedStorageSettings settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
            if (settings.Contains("Pais"))
            {
                settings["Pais"] = Pais; ;
            }
            else
            {
                settings.Add("Pais", Pais);

            }

            if (settings.Contains("Color"))
            {
                settings["Color"]=Color;
            }
            else
            {
               settings.Add("Color",Color);
            }

            if (settings.Contains("Cartografia"))
            {
                settings["Cartografia"]= Cartografia;
            }
            else
            {
                settings.Add("Cartografia",Cartografia);
            }

       
        }

    }

   public class MyConverter : IValueConverter { 


       public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
           if (value == null || parameter == null)
               return false;

           string checkValue = value.ToString();
           string targetValue = parameter.ToString();
           return checkValue.Equals(targetValue,
                    StringComparison.InvariantCultureIgnoreCase);
       }

       public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
           if (value == null || parameter == null)
               return null;

           bool useValue = (bool)value;
           string targetValue = parameter.ToString();
           if (useValue)
               return targetValue;

           return null;

       }
   }

}
